#!/user/bin/env python3
# -*- coding: utf-8 -*-
import time

import requests
import json
from PIL import Image,ImageFont,ImageDraw
import datetime
solar_terms = [
        "小寒", "大寒", "立春", "雨水", "惊蛰", "春分", "清明", "谷雨",
        "立夏", "小满", "芒种", "夏至", "小暑", "大暑", "立秋", "处暑",
        "白露", "秋分", "寒露", "霜降", "立冬", "小雪", "大雪", "冬至"
    ]
festival=['元旦','情人节','妇女节','植树节','清明节','劳动节','青年节','母亲节',]
def pilTXT(draw,xy=(0,0),txt='',ziti="font/SimHei.ttf",size=16,t=0.5,p=0.5):
    font_style = ImageFont.truetype(ziti, size, encoding="utf-8")
    l,k,w, h = font_style.getbbox(txt)
    #print(f'宽{w}高{h}')
    x,y=xy
    #draw.rectangle([(x-w*p,y-h*t), (x-w*p+w,y-h*t+h)], outline=0, width=1)

    draw.text((round(x-w*p),round(y-h*t)),txt,fill=0,font=font_style)
    print((round(x-w*p),round(y-h*t)))
class day:
    def __init__(self):
        self.gregoriandate = None#公历日期
        self.lunardate = None  #农历日期
        self.lunar_festival = None        #公历节日
        self.festival = None#天干地支年
        self.tiangandizhiyear= None#天干地支月
        self.tiangandizhimonth = None#天干地支日
        self.tiangandizhiday= None#季节
        self.lmonthname = None#生肖
        self.shengxiao= None#农历月
        self.lubarmonth= None#农历日
        self.lunarday= None#节气
        self.date=None,
        self.daycode= None,  # 日期类型，为0表示工作日、为1节假日、为2双休日、3为调休日（上班）
        self.weekday=None,
        self.cnweekday=None,
        self.info=None,
        self.isnotwork=None,
        self.tip=None# 放假提示
        self.rest=None# 拼假建议
def draw_textjieqi():
    with open('json/今日黄历.json', 'r') as source_file:
        source_data = json.load(source_file)#加载节假日文件
    HuangLi={
        'code':source_data['code'],
        'gregoriandate':source_data['result']['gregoriandate'],#公历日期
        'lunardate':source_data['result']['lunardate'],#农历日期
        'lunar_festival':source_data['result']['lunar_festival'],#农历节日
        'festival':source_data['result']['festival'],#公历节日
        'tiangandizhiyear':source_data['result']['tiangandizhiyear'],#天干地支年
        'tiangandizhimonth':source_data['result']['tiangandizhimonth'],#天干地支月
        'tiangandizhiday':source_data['result']['tiangandizhiday'],#天干地支日
        'lmonthname':source_data['result']['lmonthname'],#季节
        'shengxiao':source_data['result']['shengxiao'],#生肖
        'lubarmonth':source_data['result']['lubarmonth'],#农历月
        'lunarday':source_data['result']['lunarday'],#农历日
        'jieqi':source_data['result']['jieqi']#节气
    }
    with open('json/今日假日.json', 'r') as source_file2:
        source_data2 = json.load(source_file2)#加载节假日文件
    Jiari={
        "date": source_data2['result']['list'][0]["date"],
        "daycode": source_data2['result']['list'][0]["daycode"],#日期类型，为0表示工作日、为1节假日、为2双休日、3为调休日（上班）
        "weekday": source_data2['result']['list'][0]["weekday"],
        "cnweekday": source_data2['result']['list'][0]["cnweekday"],
        "info": source_data2['result']['list'][0]["info"],
        "isnotwork": source_data2['result']['list'][0]["isnotwork"],
        "tip": source_data2['result']['list'][0]["tip"],#放假提示
        "rest": source_data2['result']['list'][0]["rest"]#拼假建议

    }

    #字体文件
    黑体="font/SimHei.ttf"
    学校="font/Schools-Out-2.otf"
    坦克体="font/字魂铁甲坦克体(商用需授权).ttf"
    #创建一张底图,用来绘制文字
    img = Image.new("1",(400,300),color=1)
    draw = ImageDraw.Draw(img)
    #在图片上添加文字
    #fill用来设置绘制文字的颜色,(R,G,B)
    print(HuangLi['gregoriandate'])
    day=datetime.datetime.strptime(HuangLi['gregoriandate'],"%Y-%m-%d").day
    #draw.text((50,100),f"{datetime.datetime.now().day:02d}",fill=0,font=font_style_shu)
    pilTXT(draw,(100,230),f"{day}",坦克体,120,1)

    pilTXT(draw, (100, 138),
           f"{HuangLi['tiangandizhiyear']}{HuangLi['shengxiao']}年{HuangLi['lubarmonth']}{HuangLi['lunarday']}", 黑体,
           20, 1,0.5)
    pilTXT(draw, (200, 290),
           f"{HuangLi['festival']}", 黑体,
           16, 1, 0.5)
    pilTXT(draw, (200, 270),
           f"{HuangLi['lunar_festival']}{HuangLi['lmonthname']}{HuangLi['jieqi']}", 黑体,
           16, 1, 0.5)
    pilTXT(draw, (100, 235),
           f"{Jiari['cnweekday']}", 黑体,
           16, 0, 0.5)




    #draw.text((0, 0), "你好", fill=1, font=font_style)
    #画框
    draw.rectangle([(0, 0), (399, 299)], outline=0,width=8)

    #划线
    #draw.line([(0, 50), (399, 50)], fill=0)
    #保存图片
    img.save("img/draw_img_test2.bmp")





def updata_rili():
    APIKEY = 'b8eb4e41b5e444da9424b54f5c0a106f'
    url = f"https://apis.tianapi.com/lunar/index?key={APIKEY}&date=2024-10-23"
    url2=f'https://apis.tianapi.com/jiejiari/index?key={APIKEY}&date=2024-{datetime.datetime.now().month}-1&type=2'
    req=requests.get(url)
    req2=requests.get(url2)
    dict_data=json.loads(req.text)
    dict_data2=json.loads(req2.text)
    with open('json/今日黄历.json', 'w',encoding='utf-8') as json_file:
        json.dump(dict_data, json_file)
    with open('json/今日假日.json', 'w',encoding='utf-8') as json_file2:
        json.dump(dict_data2, json_file2)
    print(dict_data)




if __name__ == '__main__':

    updata_rili()
    draw_text()
    # except:
    #     print('qingqiubudao')
    #     img = Image.new("1", (400, 300), color=1)
    #     draw = ImageDraw.Draw(img)
    #     pilTXT(draw, (100, 200), f"请求失败", "font/字魂铁甲坦克体(商用需授权).ttf", 100, 1)
    #     img.save("img/draw_img_test2.bmp")